package com.example.markdownviewer;

import android.app.Dialog;
import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.DialogFragment;

import java.util.List;

public class RecentFilesDialog extends DialogFragment {

    public interface RecentFileListener {
        void onRecentFileSelected(Uri fileUri);
        void onClearRecentFiles();
    }

    private RecentFileListener listener;
    private RecentFilesManager recentFilesManager;

    @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);
        try {
            listener = (RecentFileListener) context;
        } catch (ClassCastException e) {
            throw new ClassCastException(context + " must implement RecentFileListener");
        }
        recentFilesManager = new RecentFilesManager(context);
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(requireActivity());
        builder.setTitle(R.string.recent_files);

        List<RecentFilesManager.RecentFile> recentFiles = recentFilesManager.getRecentFiles();

        if (recentFiles.isEmpty()) {
            builder.setMessage(R.string.no_recent_files);
            builder.setPositiveButton(R.string.cancel, (dialog, which) -> dismiss());
        } else {
            // 创建适配器
            RecentFilesAdapter adapter = new RecentFilesAdapter(requireActivity(), recentFiles);
            
            // 创建ListView
            ListView listView = new ListView(requireActivity());
            listView.setAdapter(adapter);
            
            // 设置点击事件
            listView.setOnItemClickListener((parent, view, position, id) -> {
                RecentFilesManager.RecentFile file = recentFiles.get(position);
                listener.onRecentFileSelected(file.getUri());
                dismiss();
            });
            
            builder.setView(listView);
            builder.setPositiveButton(R.string.cancel, (dialog, which) -> dismiss());
            builder.setNegativeButton(R.string.clear_recent_files, (dialog, which) -> {
                listener.onClearRecentFiles();
                dismiss();
            });
        }

        return builder.create();
    }

    /**
     * 最近文件列表适配器
     */
    private static class RecentFilesAdapter extends ArrayAdapter<RecentFilesManager.RecentFile> {

        public RecentFilesAdapter(Context context, List<RecentFilesManager.RecentFile> files) {
            super(context, android.R.layout.simple_list_item_1, files);
        }

        @NonNull
        @Override
        public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
            View view = convertView;
            if (view == null) {
                LayoutInflater inflater = LayoutInflater.from(getContext());
                view = inflater.inflate(android.R.layout.simple_list_item_1, parent, false);
            }

            RecentFilesManager.RecentFile file = getItem(position);
            if (file != null) {
                TextView textView = view.findViewById(android.R.id.text1);
                textView.setText(file.getFileName());
            }

            return view;
        }
    }
}